package com.css.fxfzypg.manage.sampling.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.manage.sampling.repository.entity.SamplingEntity;
import com.css.fxfzypg.manage.sampling.service.SamplingService;
import com.css.fxfzypg.manage.sampling.vo.SamplingParams;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sampling")
@Slf4j
public class SamplingController {
    @Autowired
    SamplingService samplingService;

    @Autowired
    HttpServletRequest request;

    /**
     * 新增调研点
     * @param samplingEntity
     * @return
     */
    @PostMapping("/insertSurvey")
    public RestResponse insertSurvey(@RequestBody SamplingEntity samplingEntity) {
        RestResponse response = null;
        try{
            boolean res = samplingService.insertSurvey(samplingEntity);
            if(res){
                response = RestResponse.succeed("新增成功!");
                response.setMessage("新增成功!");
            }else {
                response = RestResponse.fail("新增失败!");
            }
        }catch (Exception e){
            String errormessage = "新增失败!";
            log.error(errormessage,e);
            response=RestResponse.succeed(errormessage);
        }
        return response;
    }

    /**
     * 保存并提交
     * @param samplingEntity
     * @return
     */
    @PostMapping("/saveAndSubmit")
    public RestResponse saveAndSubmit(@RequestBody SamplingEntity samplingEntity) {
        RestResponse response = null;
        try{
            boolean res = samplingService.saveAndSubmit(samplingEntity);
            if(res){
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            }else {
                response = RestResponse.fail("保存并提交失败!");
            }
        }catch (Exception e){
            String errormessage = "保存并提交失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已提交和未提交列表
     * @param submitFlag
     * @return
     */
    @GetMapping("/querySubmitOrNot")
    public RestResponse querySubmitOrNot(HttpServletRequest request, String submitFlag,String taskCode,String userId) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = samplingService.querySubmitOrNot(submitFlag, taskCode,curPage,pageSize,userId);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取列表信息失败!",e);
            response=RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

  /**
     * 根据id更新提交状态
     * @param map
     * @return
     */
    @PostMapping("/updateIsSubmit")
    public RestResponse updateIsSubmit(@RequestBody Map<String,String> map) {
        RestResponse response = null;
        try {
            boolean res = samplingService.updateIsSubmit(map);
            if(res){
                response = RestResponse.succeed("提交成功!");
                response.setMessage("提交成功!");
            }else {
                response = RestResponse.fail("提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id查询详情页数据
     * @param id
     * @return
     */
    @GetMapping("/queryData")
    public RestResponse queryData(String id) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            SamplingEntity samplingEntity = samplingService.queryData(id);
            json.put("data",samplingEntity);
            json.put("message", "查询详情页成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errormessage = "查询详情页失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 修改详情页数据
     * @param samplingEntity
     * @return
     */
    @PostMapping("/modifyData")
    public RestResponse modifyData(@RequestBody SamplingEntity samplingEntity) {
        RestResponse response = null;
        try {
            boolean res = samplingService.modifyData(samplingEntity);
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 修改并提交
     * @param samplingEntity
     * @return
     */
    @PostMapping("/modifyAndSubmit")
    public RestResponse modifyAndSubmit(@RequestBody SamplingEntity samplingEntity) {
        RestResponse response = null;
        try {
            boolean res = samplingService.modifyAndSubmit(samplingEntity);
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 抽样调查点数据列表查询
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/querySamplingList")
    public RestResponse querySamplingData(HttpServletRequest request, SamplingParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = samplingService.querySamplingData(queryParams,curPage,pageSize);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询抽样调查点数据列表数据失败!",e);
            response=RestResponse.fail("查询抽样调查点数据列表数据失败!");
        }
        return response;
    }

    /**
     * 根据id删除数据
     * @param
     * @return
     */
   @GetMapping("/deleteById")
    public RestResponse deleteSampling(String ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            boolean res = samplingService.deleteSampling(ids);
            if(res){
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            }else {
                response = RestResponse.fail("删除失败!");
            }
        }catch (Exception e){
            log.error("删除失败!",e);
            json.put("message", "删除失败!");
            response=RestResponse.succeed(json);
        }
        return response;
    }
}